import axios from 'axios'
import router from '@/router'
import { ElMessage } from 'element-plus'

const ins = axios.create({
  baseURL: 'http://localhost:3000/ftyyptapi',
  timeout: 3000,
})

ins.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  },
)

ins.interceptors.response.use(
  response => {
    // 成功响应render-helper.mjs'

    if (response.data.state !== 200) {
      // 业务发生了错误

      ElMessage.error(response.data.msg)

      router.push('/login')

      return Promise.reject(response.data.msg)
    }

    return response
  },
  error => {
    // 失败响应
    return Promise.reject(error)
  },
)

export default ins
